package controllers

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"tizhi_front/model"
	"tizhi_front/nspel"
	"tizhi_front/response"
)

type ConsultController struct {
	Ctx iris.Context
}

func (c *ConsultController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/list/{Type:int min(0) max(3)}", "GetConsultListByType", beforeCommonToken)
	b.Handle("POST", "/type", "GetConsultType", beforeCommonToken)
}

// @Title Consult List
// @Description 根据类别获取咨询信息列表
// @Tags consult
// @Param	type path int	true "type for consult,0:身体形态 1:身体机能 2:身体素质 3:拓展测试"
// @Param	body body response.TokenInfo true "token"
// @Success 0	{object} response.ConsultList
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /consult/list/{type} [post]
func (c *ConsultController) GetConsultListByType(Type int) response.JsonResponse {
	consultQuestion := model.ConsultQuestionE{}
	consultQuestions, err := consultQuestion.SelectAllByConsultTypeOrderByOrderAsc(int64(Type))
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	res := response.ConsultList{
		Type: Type,
	}
	res.Consults = make([]response.ConsultInfo, len(consultQuestions))
	for i := 0; i < len(consultQuestions); i++ {
		res.Consults[i] = response.ConsultInfo{
			Title:   consultQuestions[i].Question,
			Content: consultQuestions[i].Answer,
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    res,
	}
}

// @Title Consult Type List
// @Description 获取咨询列表信息
// @Tags consult
// @Param	body body response.TokenInfo true "token"
// @Success 0	{object} response.ConsultTypeList
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /consult/type [post]
func (c *ConsultController) GetConsultType() response.JsonResponse {
	consultType := model.ConsultTypeE{}
	consultTypes, err := consultType.SelectAll()
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	res := response.ConsultTypeList{}
	res.ConsultTypes = make([]response.ConsultTypeInfo, len(consultTypes))
	for i := 0; i < len(consultTypes); i++ {
		res.ConsultTypes[i] = response.ConsultTypeInfo{
			Type:   int(consultTypes[i].Id),
			Title:  consultTypes[i].Name,
			Detail: consultTypes[i].Introduction,
			Label:  consultTypes[i].Label,
			PicUrl: consultTypes[i].PicUrl,
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    res,
	}
}
